﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.DBUtility;
using System.IO;


namespace SkWeb.RiskDB
{
    public partial class FormAction : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string Action = "";
                string result = "{\"success\":true}";
               
                WellInfo wellInfo = new WellInfo();
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))//获取form的Action中的参数
                {
                     Action = Request.QueryString["action"].Trim().ToLower();//去掉空格并变小写
                }
                switch (Action)
                {
                    case "add":
                        if (!string.IsNullOrEmpty(Request.Form["name"]) )//获取form中的参数
                        {
                            wellInfo.name = Request.Form["name"];
                            wellInfo.lng = Convert.ToSingle( Request.Form["lng"]);
                            wellInfo.lat = Convert.ToSingle( Request.Form["lat"]);
                            wellInfo.jb = Request.Form["jb"];
                            wellInfo.gy=Request.Form["gy"];
                            wellInfo.address=Request.Form["address"];
                            string strSql = "insert into tb_wellinfo(name,lng,lat,jb,gy,address) values('"+wellInfo.name+"',"+wellInfo.lng+","+wellInfo.lat+",'"+wellInfo.jb+"','"+wellInfo.gy+"','"+wellInfo.address+"')";
                            if (DbHelperOra.ExecuteSql(strSql) != 1)
                            {
                                result = string.Concat("{\"success\":false, \"msg\":\"", "插入出错，请联系管理员", "\"}");
                            }
                        }
                        break;
                    case "update":
                        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                        {
                            wellInfo.id=Convert.ToInt32(Request.QueryString["id"]);
                            wellInfo.name = Request.Form["name"];
                            wellInfo.lng = Convert.ToSingle(Request.Form["lng"]);
                            wellInfo.lat = Convert.ToSingle(Request.Form["lat"]);
                            wellInfo.jb = Request.Form["jb"];
                            wellInfo.gy = Request.Form["gy"];
                            wellInfo.address = Request.Form["address"];
                            string strSql = "update  tb_wellinfo set  name='"+wellInfo.name+"',lng="+wellInfo.lng+",lat="+wellInfo.lat+",jb='"+wellInfo.jb+"',gy='"+wellInfo.gy+"',address='"+wellInfo.address+"'  where id ="+wellInfo.id;
                            if (DbHelperOra.ExecuteSql(strSql) != 1)
                            {
                                result = string.Concat("{\"success\":false, \"msg\":\"", "更新出错，请联系管理员", "\"}");
                            }
                        }
                        break;
                    case "delete":
                        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                        {
                            string strSql = "delete from tb_wellinfo where id =" + Request.QueryString["id"];
                            if (DbHelperOra.ExecuteSql(strSql) != 1)
                            {
                                result = string.Concat("{\"success\":false, \"msg\":\"", "删除出错，请联系管理员", "\"}");
                            }
                        }
                        break;
                    case "upload":
                        SaveFile();
                        break;


                }

                Response.ContentType = "text/plain";
                Response.Write(result);
                Response.End();

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


        protected void SaveFile()
        {
            HttpFileCollection files = Request.Files;
            if (files != null && files.Count > 0)
            {
                HttpPostedFile file = files[0];

                string tmpPath = Server.MapPath("~/FileUpLoad/");
                string fileName = Path.GetFileName(file.FileName);
                try
                {
                    file.SaveAs(tmpPath + fileName);
                    Response.Write(@"{
                    status : 'success', 
                    msg: { 
                        Origin : '" + "/FileUpLoad/" + fileName + @"'
                    }
                }");
                }
                catch (Exception ex)
                {
                    Response.Write(@"{
                status : 'error',
                msg : '" + ex.Message + @"'
                }");
                    Response.End();
                }
            }
        }

    }
}